<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Yii2_&#25511;&#21046;&#22120;&#30340;&#25805;&#20316;&#21709;&#24212;JSON&#26684;&#24335;&#30340;&#25968;&#25454;_Koming</title>
    <style>
      html {
        padding: 1em;
        background: #fff;
        color: #222;
        font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Geneva, Verdana, sans-serif;
        font-size: 100%;
        line-height: 1.5;
      }

      h1,
      p,
      ul,
      ol,
      div,
      figure {
        margin: 0;
      }

      h1 { font-size: 1.2em; }

      ul {
        list-style: disc;
        padding-left: 1.3em;
      }

      ol {
        padding-left: 1.3em;
        list-style: decimal;
      }

      blockquote {
        border-left: 3px solid #000;
        margin: 0;
        padding: 0 0 0 1em;
      }

      pre {
        font-family: monaco, monospace;
        font-size: 0.875em;
        margin: 0;
        padding: 1rem;
        background: #eee;
        border-radius: 0.4rem;
        white-space: pre;
        word-wrap: normal;
        word-break: normal;
        overflow-x: auto;
      }

      code { border-radius: 0.4rem; }

      figure {
        display: inline-block;
        width: 100%;
        box-sizing: border-box;
      }

      figcaption { word-break: break-word; }

      img {
        display: block;
        max-width: 100%;
        margin: 0 auto;
        padding: 1px;
        border: 1px solid #eee;
      }

      a {
        color: #1b6ac9;
        text-decoration: underline;
      }

      a:active { color: #064ac9; }

      .wrapper {
        max-width: 650px;
        margin: 0 auto;
      }

      .title {
        font-size: 2em;
        margin: 0.5em 0 1em;
        line-height: 1.3;
      }

      .attachment--image {
        text-align: center;
        color: #888;
        font-size: 0.875em;
      }

      .attachment--file {
        position: relative;
        padding: 1.4em 1em 1.5em;
        border: 1px solid #ddd;
        border-bottom-width: 3px;
        border-radius: 6px;
        color: #222;
      }

      .attachment__label {
        position: absolute;
        bottom: 0;
        right: 0;
        background: #ddd;
        font-size: 10px;
        padding: 2px 6px;
        border-radius: 6px 0 4px 0;
        color: #888;
      }

      .metadata { color: #888; }
    </style>
  </head>

  <body>
    <div class="wrapper">
      <h1 class="title">Yii2_&#25511;&#21046;&#22120;&#30340;&#25805;&#20316;&#21709;&#24212;JSON&#26684;&#24335;&#30340;&#25968;&#25454;_Koming</h1>

<div class="formatted_content">
  <div>&#22312;&#19981;&#29992;&#26694;&#26550;&#24320;&#21457;&#30340;&#26102;&#20505;&#65292;&#25509;&#21475;&#36820;&#22238;json&#23383;&#31526;&#20018;&#65292;&#25105;&#20204;&#21152;&#19978;</div>
<pre>header('Content-type: application/json');</pre>
<div>&#20320;&#22914;&#26524;&#22312;&#25511;&#21046;&#22120;&#26041;&#27861;&#37324;&#20889;&#19978;&#38754;&#36825;&#20010;&#19968;&#27573;&#21516;&#26679;&#36215;&#20316;&#29992;&#12290;<br><br>Yii2&#20013;&#23450;&#20041;http&#22836;&#37096;&#20449;&#24687;&#30340;&#26159;responde&#32452;&#20214;&#12290;</div>
<pre>$headers = Yii::$app-&gt;response-&gt;headers;

$headers-&gt;add('Content-type', 'application/json');</pre>
<div>
<br>Yii2&#35201;&#36755;&#20986;JSON&#25968;&#25454;&#24456;&#36731;&#26494;&#65292;yii2&#33258;&#24049;&#23454;&#29616;&#20102;&#21709;&#24212;&#20307;&#20869;&#23481;format&#26041;&#27861;&#12290;</div>
<pre>$response = Yii::$app-&gt;response;
$response-&gt;format = \yii\web\Response::FORMAT_JSON;
$response-&gt;data = ['message' =&gt; 'hello world'];</pre>
<div>
<br>&#25511;&#21046;&#22120;&#32487;&#25215;&#20102;yii\web\Controller</div>
<div>&#36755;&#20986;JSON&#25968;&#25454;&#30340;&#24555;&#25463;&#26041;&#27861;</div>
<pre>$this-&gt;asJson($data)</pre>
<div>&#20197;&#19978;&#20195;&#30721;&#21487;&#20197;&#24456;&#36731;&#26494;&#30340;&#36755;&#20986;json</div>
</div>

    </div>
  

</body>
</html>
